<?php
namespace Netshell\Models;

class Base extends \Eloquent
{
    public function toObject()
	{
		return (object)$this->toArray();
	}
	
    public function getAllColumnsNames()
    {
        switch (\DB::connection()->getConfig('driver')) {
            case 'pgsql':
                $query = "SELECT column_name FROM information_schema.columns WHERE table_name = '" . $this->table . "'";
                $column_name = 'column_name';
                $reverse = true;
                break;

            case 'mysql':
                $query = 'SHOW COLUMNS FROM ' . $this->getTable();
                $column_name = 'Field';
                $reverse = false;
                break;
				
            case 'sqlite':
                $query = 'PRAGMA table_info(\'' . $this->getTable() . '\');';
                $column_name = 'name';
                $reverse = false;
                break;

            case 'sqlsrv':
                $parts = explode('.', $this->getTable());
                $num = (count($parts) - 1);
                $table = $parts[$num];
                $query = "SELECT column_name FROM " . \DB::connection()->getConfig('database') . ".INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = N'" . $table . "'";
                $column_name = 'column_name';
                $reverse = false;
                break;

            default: 
                $error = 'Database driver not supported: ' . \DB::connection()->getConfig('driver');
                throw new \Exception($error);
                break;
        }

        $columns = array();

		$select = \DB::select($query);
		$nope = [$this->getCreatedAtColumn(), $this->getUpdatedAtColumn(), $this->getDeletedAtColumn(), $this->getQualifiedDeletedAtColumn()];
        foreach($select as $column)
        {
			if(!in_array($column->$column_name, $nope))
				$columns[] = $column->$column_name;
        }

        if($reverse)
        {
            $columns = array_reverse($columns);
        }
		
        return $columns;
    }
}